package logic

import (
	"bluebell/dao/mysql"
	"bluebell/models"
	"bluebell/pkg/jwt"
	"bluebell/utils"
)

func SignUp(p *models.ParamSignUp) (err error) {
	//1、检查用户不存在
	err, isExist := mysql.CheckUserExist(p.Username)
	if err != nil {
		return err
	}
	if isExist {
		return utils.ErrorUserExist
	}

	//2、生成UID
	//ID := snowflake.GenID()
	//3、构造一个User实例
	newUser := models.User{
		Username: p.Username,
		Password: p.Password,
	}
	//4、保存数据库
	mysql.InsertUser(&newUser)

	return
}

func Login(loginUser *models.ParamLogin) (err error, token string) {
	var userid uint
	err, userid = mysql.VerifyAccount(loginUser)
	if err != nil {
		return err, ""
	}
	//生成token
	token, err = jwt.GenToken(userid)
	if err != nil {
		return err, ""
	}
	return nil, token
}
